package com.tangdi.production.mpaychl.service.impl.bailipay;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.tangdi.production.mpaychl.base.service.BaseTranService;
import com.tangdi.production.mpaychl.service.impl.bncp.util.MD5Util;
import com.tangdi.production.mpbase.constants.MsgCT;
import com.tangdi.production.mpbase.exception.TranException;
import com.tangdi.production.mpbase.util.MoneyUtils;
import com.tangdi.production.mpbase.util.ObjectUtil;

@Service("Bailipay_SERVICE_1014")
public class BailipayServiceImpl implements BaseTranService {
	private static final Logger logger = LoggerFactory.getLogger(BailipayServiceImpl.class);
	
	private static final String URL = "http://tg.shopshop1.com:4586/paybank.aspx";
	
	public static final String UID = "1798";	//

	public static final String NOTIFY_URL = "http://ce.imbitpay.com:8083/mpcctp/bailipay/notify-url.json";

	public static final String TOKEN = "18b2d2281c18441b9b0d7cef90aa5804";
	
	public static final String TYPE_1014 = "1014"; // 银联H5
	public static final String TYPE_1030 = "1030"; // 网关支付  
	
	@Override
	public Map<String, Object> execute(Map<String, Object> param) throws TranException {
		logger.info("进入Bailipay_SERVICE处理接口,参数:[{}]", param);
		try {
			String parter = UID;
			String payAmt = ObjectUtil.getString(param.get("payAmt"));
			logger.info("支付金额[{}分]", payAmt);
			payAmt = MoneyUtils.toStrYuan(payAmt);
			logger.info("支付金额转换为元[{}]", payAmt);
			String value = payAmt;
			String type = TYPE_1014;
//			String callbackurl = URLEncoder.encode(NOTIFY_URL, "gb2312");
//			String hrefbackurl = URLEncoder.encode(param.get("frontUrl") == null ? "" : "" + param.get("frontUrl"), "gb2312");
			String callbackurl = NOTIFY_URL;
			String hrefbackurl = "" + param.get("frontUrl");
			String orderid = "" + param.get("prdordno");
			
			StringBuilder encodeStr = new StringBuilder();
			encodeStr.append("parter=").append(parter);
			encodeStr.append("&type=").append(type);
			encodeStr.append("&value=").append(value);
			encodeStr.append("&orderid=").append(orderid);
			encodeStr.append("&callbackurl=").append(callbackurl);
			encodeStr.append(TOKEN);
			
			String sign = MD5Util.encode(encodeStr.toString().getBytes());

			StringBuilder returtnHtml = new StringBuilder();
			returtnHtml.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\" /></head><body><script language=\"javascript\">window.onload=function(){document.payfrm.submit();}</script>");
			returtnHtml.append("<form id=\"payfrm\" name=\"payfrm\" action=\"").append(URL).append("\" method=\"post\" >");
			returtnHtml.append("<input type=\"hidden\" name=\"parter\" value=\"").append(parter).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"type\" value=\"").append(type).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"value\" value=\"").append(value).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"orderid\" value=\"").append(orderid).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"callbackurl\" value=\"").append(callbackurl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"hrefbackurl\" value=\"").append(hrefbackurl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"payerIp\" value=\"").append("127.0.0.1").append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"payerID\" value=\"").append("0001798").append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"sign\" value=\"").append(sign).append("\" />");
			returtnHtml.append("</form></body></html>");
			
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_PROCESSING);
			backMap.put("contentType", MsgCT.DATA_TYPE_HTML);
			backMap.put("busContent", returtnHtml.toString());
			return backMap;
		} catch (Exception e) {
			logger.error("渠道Bailipay调用异常:{}", e.getMessage(), e);
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_FAIL);
			backMap.put("msg", "渠道Bailipay调用异常:" + e.getMessage());
			return backMap;
		}
	}
	
	public static void main(String[] args) throws TranException {
		Map<String, Object> params = new HashMap<>();
		params.put("payAmt", "16");
		params.put("frontUrl", "http://58.220.30.99:8081/qrcode-starter/returnTest");
		params.put("prdordno", System.currentTimeMillis() + RandomStringUtils.randomAlphabetic(5));
		
		Map<String, Object> returnMap = new BailipayServiceImpl().execute(params);
		
		System.err.println(returnMap.toString());
		
		System.out.println();
		System.out.println(returnMap.get("busContent"));
		
		String enStr = "1234567890abcdefghijklmnopqrstuvwxyz";
		System.out.println(MD5Util.encode(enStr.getBytes()));
	}

}
